package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentFixedAssetsBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberAssetInfoListBean;
import com.sgb.member.ui.activity.ClientPropertyActivity;
import com.sgb.member.ui.adapter.FixedAssetsAdapter;
import com.sgb.member.view_model.FixedAssetsModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by：冯齐 on 2021/1/26 18:01
 * Description： 固定资产
 */
public class FixedAssetsFragment extends AppLazyFragment {

    private FragmentFixedAssetsBinding mBind;
    @Inject
    FixedAssetsModel mModel;
    private CommonPopEntity mAll;
    //资产类型弹窗
    private String mNo4;

    private FixedAssetsAdapter mFixedAssetsAdapter;

    public static FixedAssetsFragment newInstance() {
        FixedAssetsFragment fragment = new FixedAssetsFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_fixed_assets, container, false);
        return mBind.getRoot();
    }

    @Override
    protected void initView() {
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
        initRecycler();
    }

    //Recycler数据处理
    private void initRecycler() {
        TextView textView = mBind.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBind.recyclerView.setPullRefreshEnabled(true);
        mBind.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBind.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBind.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mFixedAssetsAdapter = new FixedAssetsAdapter(getContext(), null);
        mBind.recyclerView.setAdapter(mFixedAssetsAdapter);
        mBind.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
    }

    //请求固定资产列表数据信息
    public void loadData(boolean isLoadMore) {
        String searchText = getActivity() == null ? "" : ((ClientPropertyActivity) getActivity()).getSearchText();
        mModel.getMemberAssetInfoList(isLoadMore,  searchText, mNo4);
    }

    @Override
    protected void onLazyLoad() {
        //资质类型接口请求
        mModel.getgetAssetType();
    }

    //资产类型返回数据
    public void setQualifyTypeTreeList(List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        data.add(0, mAll);
        mBind.tabLayout2.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBind.tabLayout2.addTab(mBind.tabLayout2.newTab().setText(data.get(i).getTypeName()));
        }
        mBind.tabLayout2.setNormalColor(R.color.color_tab_two);
        mBind.tabLayout2.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mNo4 = data.get(tab.getPosition()).getNo();
                loadData(false);
            }
        });
        loadData(false);
    }

    //固定资产类表返回接口
    public void setMemberAssetInfoList(List<MemberAssetInfoListBean> list, boolean isLoadMore, boolean isLastPage) {
        mBind.recyclerView.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            mBind.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                mFixedAssetsAdapter.addDatas(list);
            }
            mBind.recyclerView.setNoMore(isLastPage);
        } else {
            mBind.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                mBind.emptyView.setVisibility(View.GONE);
                mFixedAssetsAdapter.setDatas(list);
            } else {
                mFixedAssetsAdapter.clearDatas();
                mBind.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    //网络请求空数据处理
    public void showEmpty() {
        mFixedAssetsAdapter.clearDatas();
        mBind.emptyView.setVisibility(View.VISIBLE);
    }
}
